package com.zz.info.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zz.common.config.Constant;
import com.zz.common.controller.BaseController;
import com.zz.common.utils.PageUtils;
import com.zz.common.utils.Query;
import com.zz.common.utils.R;
import com.zz.info.domain.Info;
import com.zz.info.service.InfoService;


@Controller
@RequestMapping("/info/admin")
public class InfoController extends BaseController {
	@Autowired
    InfoService infoService;

	@GetMapping()
	@RequiresPermissions("info:admin:list")
	String bContent() {
		return "info/admin/main";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("info:admin:list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<Info> infoList = infoService.list(query);
		int total = infoService.count(query);
		PageUtils pageUtils = new PageUtils(infoList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("info:admin:add")
	String add() {
		return "info/admin/add";
	}

	@GetMapping("/edit/{cid}")
	@RequiresPermissions("info:admin:edit")
	String edit(@PathVariable("cid") Long cid, Model model) {
		Info info = infoService.get(cid);
		model.addAttribute("info", info);
		return "info/admin/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequiresPermissions("info:admin:add")
	@PostMapping("/save")
	public R save(Info info) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if (info.getAllowComment() == null) {
			info.setAllowComment(0);
		}
		if (info.getAllowFeed() == null) {
			info.setAllowFeed(0);
		}
		if(null==info.getType()) {
			info.setType("article");
		}
		info.setGtmCreate(new Date());
		info.setGtmModified(new Date());
		int count;
		if (info.getCid() == null || "".equals(info.getCid())) {
			count = infoService.save(info);
		} else {
			count = infoService.update(info);
		}
		if (count > 0) {
			return R.ok().put("cid", info.getCid());
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@RequiresPermissions("info:admin:edit")
	@ResponseBody
	@RequestMapping("/update")
	public R update( Info info) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		info.setGtmCreate(new Date());
		infoService.update(info);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("info:admin:delete")
	@PostMapping("/remove")
	@ResponseBody
	public R remove(Long id) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if (infoService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("info:admin:bdelete")
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] cids) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		infoService.batchRemove(cids);
		return R.ok();
	}
}

